package com.gzy.xuexiziliao.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gzy.xuexiziliao.common.result.PageResult;
import com.gzy.xuexiziliao.mapper.InformationCategoriesMapper;
import com.gzy.xuexiziliao.mapper.InformationMapper;
import com.gzy.xuexiziliao.pojo.entity.Doctor;
import com.gzy.xuexiziliao.pojo.entity.Information;
import com.gzy.xuexiziliao.service.InformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class InformationServiceImpl implements InformationService {
    @Autowired
    private InformationMapper informationMapper;

    @Autowired
    private InformationCategoriesMapper informationCategoriesMapper;

    @Override
    @Transactional // 添加事务注解确保数据一致性
    public Information save(Information information) {
        // 设置创建时间和更新时间
        information.setCreateTime(LocalDateTime.now());
        information.setUpdateTime(LocalDateTime.now());
        information.setStatus(0);
        // 保存资讯信息
        int categoryId = information.getCategoryId();
        String categoryName = informationCategoriesMapper.getcategoryName(categoryId);
        information.setCategoryName(categoryName);
        // 保存资讯分类信息
        informationMapper.save(information);
        return information;

    }

    @Override
    public void update(Information information) {
        information.setUpdateTime(LocalDateTime.now());
        informationMapper.update(information);
    }

    @Override
    public void delete(List<Integer> ids) {
        informationMapper.deleteByIds(ids);
    }

    @Override
    public List<Information> getBycategoryId(Integer id) {
        List<Information> informationList = informationMapper.getBycategoryId(id);
        return informationList;
    }

    @Override
    public PageResult page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
        //1.设置分页参数
        PageHelper.startPage(page,pageSize);
        //2.执行查询
        List<Information> InformationList =informationMapper.list(name, gender, begin, end);
        Page<Information> p=(Page<Information>) InformationList;

        //3.封装对象
        PageResult pageResult=new PageResult(p.getTotal(),p.getResult());
        return pageResult;

    }

    @Override
    public Information getBYTitle(Information information) {
        Information byTitle = informationMapper.getBYTitle(information);
        return byTitle;

    }

    @Override
    public List<Information> getcategoryId(Integer categoryid) {
        List<Information> bycategoryId = informationMapper.getcategoryId(categoryid);
        return bycategoryId;
    }

    @Override
    public List<Doctor> getstatus(Integer id) {
        List<Doctor> bystatus = informationCategoriesMapper.getstatus(id);
        return bystatus;
    }

    @Override
    public Information getBYInformationId(int informationId) {
        Information byInformationId = informationMapper.getBYInformationId(informationId);
        return byInformationId;
    }
}

